package com.yuchen.ishop.comment;

import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.yuchen.core.MyPager;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Comment;
import com.yuchen.jaxrs.Responses;
import com.yuchen.jaxrs.ResponseUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 *  @author Jack Q
 */
@Transactional
@Path("commentService")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class CommentServiceImpl implements CommentService {

    private final  static Logger LOG = Logger.getLogger(CommentService.class);

    @Autowired
    private BaseDao dao;

    //路径：services/orderCommentService/find/1/pager/1/2
    @GET
    @Path("find/{orderId}/pager/{pageNo}/{pageSize}")
    @Transactional(readOnly = true)
    public MyPager find(@PathParam("orderId") Long order_id,@PathParam("pageNo") int pageNo,@PathParam("pageSize") int pageSize) {
        MyPager myPager = new MyPager();
        myPager.setPageSize(pageSize);
        myPager.setPageNo(pageNo);
        Condition c = Cnd.where("orderId", "=", order_id).desc("createTime");
        myPager = dao.queryPager(Comment.class,c,myPager);
        return myPager;
    }

    //路径：services/orderCommentService/add
    //说明：保存评论
    @POST
    @Path("add")
    public Responses add(Comment comment){
        try{
            dao.insert(comment);
            return ResponseUtils.success();
        }catch(Exception e){
            LOG.error("保存订单评论错误："+e.getMessage());
            return ResponseUtils.error();
        }

    }

    //路径：services/delete/{id}
    //说明：删除评论
    @POST
    @Path("delete/{id}")
    public Responses delete(@PathParam("id") Long id){
        try{
            dao.delete(Comment.class,id);
            return ResponseUtils.success();
        }catch(Exception e){
            LOG.error("删除订单评论错误："+e.getMessage());
            return ResponseUtils.error();
        }
    }

    //路径：services/update
    //说明：修改订单评论
    @POST
    @Path("update")
    public Responses update(Comment comment){
        try{
            dao.updateIgnoreNull(comment);
            return ResponseUtils.success();
        }catch(Exception e){
            LOG.error("修改订单评论错误："+e.getMessage());
            return ResponseUtils.error();
        }
    }


}
